/*** Includes *****************************************************************/
#include "fsl_debug_console.h"
#include "fsl_ftm.h"

#include "clock_config.h"
#include "pin_mux.h"
#include "peripherals.h"

#include "pit.h"
#include "motor.h"
#include "servo.h"
#include "ov7725.h"

/*** Definitions **************************************************************/

/*** Prototypes ***************************************************************/

/*** Variables ****************************************************************/
extern bool PIT_ReadyFlag;      // PIT中断标志

/* 电机，编码器参数结构体 */
mSpeed_t motor = {
    .pwm = 0,
    .count = 0,
    .dir = false,
};

/* 创建摄像头缺省配置表 */
camConf_t camera = {
    .FPS = 50,          //帧率
    .CNST = 128,        //曝光度
    .AutoAEC = true,    //自动曝光
    .AutoAGC = true,    //自动增益
    .AutoAWB = true,    //自动白平衡
};

/*!
 * @brief Main function
 */
int main(void)
{
    /* Board pin, clock, peripherals init */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitPeripherals();

    /* 调试串口初始化 */
    uint32_t uartClkSrcFreq = CLOCK_GetFreq(UART4_CLK_SRC);
    DbgConsole_Init((uint32_t) UART4, 115200, DEBUG_CONSOLE_DEVICE_TYPE_UART, uartClkSrcFreq);
    PRINTF("\r\n********** VisionAuto Project **********\r\n");

    /* 全局定时器初始化 */
    PIT_Configuration();

    /* 板级设备初始化 */
    SD5_Config();
    CAM_Config();
    CAM_UpdateProfile(&camera);

    /* PWM测试，占空比20%，成功后屏蔽此代码 */
    motor.pwm = 20;
    Motor_ChangeDuty(motor);

    SD5_ChangeDuty(3000);

    while (1)
    {
        /* Wati for the counter value is ready. */
        if (PIT_ReadyFlag == true)
        {
            PIT_ReadyFlag = false;

            /* Print the value. */
//            if (motor.dir)
//                PRINTF("Get current counter: +%d\r\n", motor.count);
//            else
//                PRINTF("Get current counter: -%d\r\n", motor.count);
        }  

        CAM_ShanWai_SendPicture();
    }
}
